/**
 * 
 */
package com.xbang.attendance.entity.product;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import enumeration.MeasureUnit;
import enumeration.ProductStatus;

/**
 * 产品信息
 * 
 * @author dev.zhang
 * 
 */
@Entity
public class Product implements Serializable {

	private static final long serialVersionUID = -14888150089353008L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	/** 产品名称 */
	private String productName;

	/** 产品编号 */
	private String productCode;

	/** 参考价格 */
	private BigDecimal refPrice;

	/** 产品规格 */
	private String specification;

	/** 产品材质 */
	private String material;

	/** 计量单位 */
	@Enumerated(EnumType.STRING)
	private MeasureUnit measureUnit;

	/** 产品状态 */
	@Enumerated(EnumType.STRING)
	private ProductStatus productStatus;

	/** 产品类别 */
	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, targetEntity = ProductCategory.class, fetch = FetchType.LAZY)
	@JoinColumn(columnDefinition = "product_category", referencedColumnName = "id")
	private ProductCategory productCategory;

	/**
	 * @param id
	 *            id
	 * @param productName
	 *            name
	 * @param productCode
	 *            code
	 * @return
	 */
	public Product copyProperties(Integer id, String productName, String productCode) {
		this.id = id;
		this.productName = productName;
		this.productCode = productCode;
		return this;
	}

	public Product copyPropereties(Product sourceProduct) {
		if (sourceProduct.getProductCategory() != null) {
			this.setProductCategory(sourceProduct.getProductCategory());
		}
		if (sourceProduct.getProductName() != null && !sourceProduct.getProductName().isEmpty()) {
			this.setProductName(sourceProduct.getProductName());
		}

		if (sourceProduct.getSpecification() != null && !sourceProduct.getSpecification().isEmpty()) {
			this.setSpecification(sourceProduct.getSpecification());
		}

		if (sourceProduct.getMeasureUnit() != null) {
			this.setMeasureUnit(sourceProduct.getMeasureUnit());
		}

		return this;
	}

	public Product() {
		// productCategory = new ProductCategory();
	}

	public boolean isTrueMeasureUnit(String measureUnit) {
		if (measureUnit == null || measureUnit.isEmpty()) {
			return false;
		}
		try {
			MeasureUnit.valueOf(measureUnit);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public String getMeasureUnitDisplay() {

		if (measureUnit == null) {
			return "";
		}
		if (measureUnit.compareTo(MeasureUnit.Barrel) == 0) {
			return "桶";
		}

		return "";
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the productName
	 */
	public String getProductName() {
		return productName;
	}

	/**
	 * @param productName
	 *            the productName to set
	 */
	public void setProductName(String productName) {
		this.productName = productName;
	}

	/**
	 * @return the productCode
	 */
	public String getProductCode() {
		return productCode;
	}

	/**
	 * @param productCode
	 *            the productCode to set
	 */
	public void setProductCode(String productCode) {
		this.productCode = productCode;
	}

	/**
	 * @return the refPrice
	 */
	public BigDecimal getRefPrice() {
		return refPrice;
	}

	/**
	 * @param refPrice
	 *            the refPrice to set
	 */
	public void setRefPrice(BigDecimal refPrice) {
		this.refPrice = refPrice;
	}

	/**
	 * @return the measureUnit
	 */
	public MeasureUnit getMeasureUnit() {
		return measureUnit;
	}

	/**
	 * @param measureUnit
	 *            the measureUnit to set
	 */
	public void setMeasureUnit(MeasureUnit measureUnit) {
		this.measureUnit = measureUnit;
	}

	/**
	 * /**
	 * 
	 * @return the productCategory
	 */
	public ProductCategory getProductCategory() {
		return productCategory;
	}

	/**
	 * @param productCategory
	 *            the productCategory to set
	 */
	public void setProductCategory(ProductCategory productCategory) {
		this.productCategory = productCategory;
	}

	/**
	 * @return the specification
	 */
	public String getSpecification() {
		return specification;
	}

	/**
	 * @param specification
	 *            the specification to set
	 */
	public void setSpecification(String specification) {
		this.specification = specification;
	}

	/**
	 * @return the material
	 */
	public String getMaterial() {
		return material;
	}

	/**
	 * @param material
	 *            the material to set
	 */
	public void setMaterial(String material) {
		this.material = material;
	}

	/**
	 * @return the productStatus
	 */
	public ProductStatus getProductStatus() {
		return productStatus;
	}

	/**
	 * @param productStatus
	 *            the productStatus to set
	 */
	public void setProductStatus(ProductStatus productStatus) {
		this.productStatus = productStatus;
	}

	/***
	 * 根据传进的字符串，判断它的材质
	 * 
	 * @param unit
	 * @return
	 */
	public MeasureUnit getMeasureUnit(String unit) {
		return measureUnit.Barrel;
	}

}
